import { Prisma, PrismaClient } from '@prisma/client/index'
export const extendedPrismaClient = new PrismaClient().$extends({
    model: {
        $allModels: {
            async findAndCount<T, A>(
                this: T,
                args: Prisma.Args<T, 'findMany'>,
            ): Promise<{
                rows: Prisma.Result<T, Prisma.Args<T, 'findMany'>, 'findMany'>;
                total: number;
            }> {
                const context: any = Prisma.getExtensionContext(this);
                // 不使用事务，查询会更快
                const [rows, total] = await Promise.all([
                    context.findMany(args),
                    context.count({ where: args.where }),
                ]);

                return { rows, total };

                // 使用事务查询的写法
                // const [rowss, total1] = await extendedPrismaClient.$transaction([
                //     context.findMany(args),
                //     context.count({ where: args.where }),
                // ]);
                // return { rowss, total1 };
            },

            async cheshi<T, A>() {},
        }
    },
})

export type ExtendedPrismaClient = typeof extendedPrismaClient;